iT邦幫忙

2025 iThome 鐵人賽

DAY 12
0
Cloud Native

《30 天 Cloud Native:AI 行程規劃後端開發》系列 第 12

Day12 - Agent 工具鏈實驗:多步驟任務與 Tool 呼叫測試

  • 分享至 

  • xImage
  •  

Agent 行程規劃初探:從輸入到 JSON 輸出

今天來想想要怎麼讓 Agent 規劃行程,初步的構想是使用者輸入地點與預計待的天數,然後 Agent 要規劃出行程並輸出成 JSON 格式回傳。

將這些需求提供給 AI 產生完整的測試程式,他幫我產生了以下輸入範例:

Locations = "Tokyo", "Kyoto", "Osaka"
Interests = "culture", "food", "nature"
Pace = "relaxed"

範例輸出結果如下:

            "title": "My Journey Through Japan",
            "itinerary": [
              {
                "date": "2025-09-10",
                "location": "Tokyo",
                "activities": [
                  "Explore Tokyo's culture scene",
                  "Explore Tokyo's food scene",
                  "Explore Tokyo's nature scene"
                ],
                "tips": "Enjoy a relaxed pace in Tokyo. Try local food and respect cultural sites."
              },
              {
                "date": "2025-09-11",
                "location": "Kyoto",
                "activities": [
                  "Explore Kyoto's culture scene",
                  "Explore Kyoto's food scene",
                  "Explore Kyoto's nature scene"
                ],
                "tips": "Enjoy a relaxed pace in Kyoto. Try local food and respect cultural sites."
              },
              {
                "date": "2025-09-12",
                "location": "Osaka",
             ...

說了什麼但實際上什麼都沒說,看了看產生出來的 Agent Tool,裡面的邏輯就只是解析使用者想去的地點和有興趣的活動後,用兩個迴圈將這些資訊一個一個的放進 JSON 格式內,才變成現在這樣的結果。

應該是規格太不清楚的關係導致,看來我的詠唱技能需要多練練呢。

多工具協作的挑戰:Agent 能否同時呼叫多個 Tool?

這時我就好奇,Agent 是不是一次只能使用一種工具呢?如果他一次只會呼叫一次我提供的 Tool,那就會不符合這個 AI 行程規劃的需求,因為 AI 要做的事情有:

  1. 規劃出大致上的行程
  2. 呼叫 Google Map API 確認交通時間
  3. 將最後的結果輸出成 JSON

未來會需要請 AI 依照情況呼叫不同的 Tool,因此先來做個測試,定一個簡單的 Agent:

def hello(a: int, b: int) -> int:
    return a + b

def goodbye(a: int, b: int) -> int:
    return a - b

root_agent = Agent(
	...
    instruction=(
        "You are a helpful agent who can answer user questions"
    ),
    tools=[hello, goodbye],
)

這邊要注意的是,如果沒有指定 hellogoodbye 函數的參數和回傳型態的話, Agent 還是會把資料放進去,但因為不知道型態會遇到以下錯誤:

ValueError: Failed to parse the parameter a of function hello for automatic function calling. Automatic function calling works best with simpler function signature schema, consider manually parsing your function declaration for function hello.

測試 Tool 呼叫能力:Prompt 詠唱技巧

那先來試著輸入 Prompt 吧:

Please give me the result of hello(hello(50, 50), goodbye(200, 10))

得到了以下結果:

I can only make one function call at a time and the output of one function cannot be used as the input to another function in the same turn.

不過有趣的是如果多執行幾次就成功了:

The result is 290.

如果逐步一個一個請他執行的話:

First, get the result of hello(50, 50) and assign it to X. Then, get the result of goodbye(200, 10) and assign it to Y. Finally, get the result of hello(X, Y)

結果為:

The result of hello(50, 50) is 100.
The result of goodbye(200, 10) is 190.
The result of hello(100, 190) is 290.

所以只要下對 Prompt 就可以請 Agent 執行對應的 Tool ,接下來再依照需求想些合適的 Prompt 吧。


上一篇
Day11 - 從本地到雲端:Vertex AI Agent Engine 自動部署全攻略
下一篇
Day13 - 從 Prompt 到 JSON:Gemini API 行程規劃實作與錯誤排查全紀錄
系列文
《30 天 Cloud Native:AI 行程規劃後端開發》21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言